import os
from lib import constants as C
import ConfigParser


class AnsibleSettings(object):
    def __init__(self):
        self.config = self.ansible()

    def ansible(self):
        """ Reads the settings from the ansible-cmdb.ini file """
        config_file_realpath = C.CONFIG_PATH + 'ansible_data.ini'
        if (os.path.exists(config_file_realpath)):
            config = ConfigParser.SafeConfigParser()
            config.read(config_file_realpath)
            return config
        else:
            return

    @property
    def sessions(self):
        sessions = self.config.getint('ansible_cmdb', 'sessions')
        return sessions

    @property
    def cache_max_age(self):
        cache_max_age = self.config.getint('ansible_cmdb', 'cache_max_age')
        return cache_max_age

    @property
    def cache_base_facts(self):
        cache_base_facts = self.config.get('ansible_cmdb', 'cache_base_facts')
        return cache_base_facts

    @property
    def cache_extra_facts(self):
        cache_extra_facts = self.config.get('ansible_cmdb', 'cache_extra_facts')
        return cache_extra_facts

    @property
    def hosts_batch_step(self):
        hosts_batch_step = self.config.getint('ansible_cmdb', 'hosts_batch_step')
        return hosts_batch_step

    @property
    def inventory(self):
        inventory = self.config.get('ansible_cmdb', 'inventory')
        return inventory


class MySQLSettings(object):
    def __init__(self):
        self.config = self.mysql()

    def mysql(self):
        """ Reads the settings from the mysql.ini file """
        config_file_realpath = C.CONFIG_PATH + 'mysql.ini'
        if (os.path.exists(config_file_realpath)):
            config = ConfigParser.SafeConfigParser()
            config.read(config_file_realpath)
            return config
        else:
            return

    @property
    def mysql_host(self):
        host = self.config.get('mysql', 'host')
        return host

    @property
    def mysql_username(self):
        username = self.config.get('mysql', 'username')
        return username

    @property
    def mysql_password(self):
        password = self.config.get('mysql', 'password')
        return password

    @property
    def mysql_database(self):
        database = self.config.get('mysql', 'database')
        return database

    @property
    def mysql_socket(self):
        socket = self.config.get('mysql', 'socket')
        return socket


def main():
    test_settings = AnsibleSettings()
    print test_settings.sessions
    print test_settings.cache_max_age
    print test_settings.cache_base_facts
    print test_settings.cache_extra_facts
    print test_settings.hosts_batch_step
    print test_settings.inventory

    test_settings = MySQLSettings()
    print test_settings.mysql_host
    print test_settings.mysql_username
    print test_settings.mysql_password
    print test_settings.mysql_database
    print test_settings.mysql_socket

if __name__ == '__main__':
    main()
